﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.WinCore;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace SmartPlantApp.ViewModels
{
    public class SelectThemeViewModel : DialogViewModelBase, IDialogAware
    {
        private string regPath;
        private ObservableCollection<ComboBoxItemModel<string>> _comboBoxItems;
        private ComboBoxItemModel<string> _selectObj;
        private ICommand _switchThemeCommand;

        public SelectThemeViewModel(IDialogService dialogService) : base(dialogService)
        {
            regPath = WinRegister.regPath;
            this.Title = "切换系统皮肤";
            _comboBoxItems = new ObservableCollection<ComboBoxItemModel<string>>
            {
                new ComboBoxItemModel<string>{ Name="Crystal" },
                new ComboBoxItemModel<string>{ Name="Fluent" },
                new ComboBoxItemModel<string>{ Name="Green" },
                new ComboBoxItemModel<string>{ Name="Office_Black" },
                new ComboBoxItemModel<string>{ Name="Office_Blue" },
                new ComboBoxItemModel<string>{ Name="Office_Silver" },
                new ComboBoxItemModel<string>{ Name="Office2013" },
                new ComboBoxItemModel<string>{ Name="Office2016" },
                new ComboBoxItemModel<string>{ Name="Summer" },
                new ComboBoxItemModel<string>{ Name="Vista" },
                new ComboBoxItemModel<string>{ Name="VisualStudio2013" },
                new ComboBoxItemModel<string>{ Name="VisualStudio2019" },
                new ComboBoxItemModel<string>{ Name="Windows7" },
                new ComboBoxItemModel<string>{ Name="Windows8" }
            };
        }


        public ObservableCollection<ComboBoxItemModel<string>> Themes
        {
            get { return _comboBoxItems; }
            set { SetProperty(ref _comboBoxItems, value); }
        }

        public ComboBoxItemModel<string> SelectObj
        {
            get { return _selectObj; }
            set { SetProperty(ref _selectObj, value); }
        }

        public ICommand SwitchThemeCommand
        {
            get
            {
                if (_switchThemeCommand != null) return _switchThemeCommand;
                _switchThemeCommand = new DelegateCommand<object>(onSwitchThemeCommand);
                return _switchThemeCommand;
            }
        }

        private void onSwitchThemeCommand(object obj)
        {
            if (SelectObj == null)
                return;

            string themeName = SelectObj.Name;

            Application.Current.Resources.MergedDictionaries.Clear();
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
            });
            //Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            //{
            //    Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.Docking.xaml", UriKind.RelativeOrAbsolute)
            //});
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.ScheduleView.xaml", UriKind.RelativeOrAbsolute)
            });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("/Telerik.Windows.Themes." + themeName + ";component/Themes/Telerik.Windows.Controls.RibbonView.xaml", UriKind.RelativeOrAbsolute)
            });

            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/SmartPlant.Core;component/Resources/SmartStyles.xaml", UriKind.RelativeOrAbsolute)
            });

            WinRegister.SetStringValueKey(regPath, "ThemeName", themeName);
            onCloseDialogCommand("OK");
        }
    }
}
